package com.example.kbase.fragment.subFragment;


import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.kbase.R;
import com.example.kbase.fragment.MBaseFragment;
import com.example.kbase.util.Constant;
import com.example.kbase.util.L;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Request;

/**
 * A simple {@link Fragment} subclass.
 */
public class ModifyMBaseFragment extends Fragment {
    private  View view;
    private ImageView backToMBase;
    private EditText articleName;
    private EditText userId;
    private EditText categoryId;
    private EditText author;
    private EditText content;
    private Button add;
    private ProgressDialog dialog;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_modify_mbase,null);
        backToMBase = view.findViewById(R.id.backToMBase);
        articleName = view.findViewById(R.id.articleName);
        userId = view.findViewById(R.id.user_id);
        categoryId = view.findViewById(R.id.category_id);
        author = view.findViewById(R.id.author);
        content = view.findViewById(R.id.content);
        add = view.findViewById(R.id.addA);

        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        final String name = articleName.getText().toString().trim();
        final String user_id = userId.getText().toString().trim();
        final String category_id = categoryId.getText().toString().trim();
        final String authors = author.getText().toString().trim();
        final String contents = content.getText().toString().trim();

        backToMBase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction.setCustomAnimations(
                        R.animator.fragment_slide_left_enter,
                        R.animator.fragment_slide_left_exit,
                        R.animator.fragment_slide_right_exit,
                        R.animator.fragment_slide_right_enter).
                        replace(R.id.content_layout,new MBaseFragment()); //replace实现返回上一层效果
                //addToBackStack(null);   当前界面加入回退栈
                transaction.commit();
            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                modify(name,authors,contents,user_id,category_id);
            }
        });
        return view;
    }
    private void modify(String author,String name,String content,String userId,String categoryId){
        String url = Constant.BaseUrl+ "/article/update_article.do";
        OkHttpUtils
                .post()
                .url(url)
                .addParams("categoryId",categoryId)
                .addParams("userId",userId)
                .addParams("name",name )
                .addParams("author", author)
                .addParams("content",content)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(getActivity(), "modify异常", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onBefore(Request request, int id) {
                        super.onBefore(request, id);
                        dialog = new ProgressDialog(getActivity());
                        dialog.setTitle("Tips");
                        dialog.setMessage("...modify...");
                        dialog.show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        dialog.dismiss();
                        L.e(response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if(jsonObject.getInt("status")==0){
                                transaction.setCustomAnimations(
                                        R.animator.fragment_slide_left_enter,
                                        R.animator.fragment_slide_left_exit,
                                        R.animator.fragment_slide_right_exit,
                                        R.animator.fragment_slide_right_enter).
                                        replace(R.id.content_layout,new MBaseFragment()).
                                        addToBackStack("mBaseFragment");
                                Toast.makeText(getActivity(), "modify success.", Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(getActivity(), jsonObject.getString("msg"), Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                });
    }



}
